﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;
using System.Data;

namespace DAL
{
    public class OrderService
    {
        //订单全查
        public static List<Orderss> SelectAll()
        {
            string sql = "select c.Orderid 订单id,a.Fname 名称,d.NickName  用户,d.Phone 电话,c.Num 数量,c.Allprice 价格,d.Addc 地址,OrderTime 下单时间,c.Status 配送状态 from[Food] a join FoodType b on a.Typeid = b.Typeid  join Orderss c on c.Foodid = a.Foodid  join UserInfo d on c.Uid = d.Uid;";
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            List<Orderss> list = new List<Orderss>();
            while (dr.Read())
            {
                Orderss o = new Orderss();
                o.Orderid = dr.GetInt32(0);
                o.Fname = dr.GetString(1);
                o.NickName = dr.GetString(2);
                o.Phone = dr.GetString(3);
                o.Num = dr.GetInt32(4);
                o.Allprice = dr.GetDecimal(5);
                o.Adds = dr.GetString(6);
                o.OrderTime = dr.GetDateTime(7);
                o.Status = dr.GetString(8);

                list.Add(o);


            }
            dr.Close();
            return list;
        }
        //分页查询
        public static List<Orderss> SelectByPage(int count, int pageIndex)
        {
            string sql = string.Format("select top {0}  c.Orderid 订单id,a.Fname 名称,d.NickName  用户,d.Phone 电话,c.Num 数量,c.Allprice 价格,d.Addc 地址,OrderTime 下单时间,c.Status 配送状态  from Food a " +
            " join FoodType b"+" on a.Typeid = b.Typeid"+" join Orderss c on c.Foodid = a.Foodid"+" join UserInfo d on c.Uid = d.Uid"+" where a.Foodid not in (select top {1} Foodid from food)",
            count, 10 * (pageIndex - 1)); 
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            List<Orderss> list = new List<Orderss>();
            while (dr.Read())
            {
                Orderss o = new Orderss();
                o.Orderid = dr.GetInt32(0);
                o.Fname = dr.GetString(1);
                o.NickName = dr.GetString(2);
                o.Phone = dr.GetString(3);
                o.Num = dr.GetInt32(4);
                o.Allprice = dr.GetDecimal(5);
                o.Adds = dr.GetString(6);
                o.OrderTime = dr.GetDateTime(7);
                o.Status = dr.GetString(8);
                list.Add(o);
            }
            dr.Close();
            return list;
        }
        //修改状态
        public static bool Edit(string ID)
        {
            string sql;
            //string sql = string.Format("update Orderss set[Status] = b.Statu  from(select case when[Status] = '未配送'  then '已配送' else '未配送'  end Statu  from Orderss where Orderid = '{0}' )b   where Orderid = '{0}'", ID);
            string sq = string.Format("select c.Orderid 订单id,a.Fname 名称,d.NickName  用户,d.Phone 电话,c.Num 数量,c.Allprice 价格,d.Addc 地址,OrderTime 下单时间,c.Status 配送状态 from[Food] a join FoodType b on a.Typeid = b.Typeid  join Orderss c on c.Foodid = a.Foodid  join UserInfo d on c.Uid = d.Uid where Orderid = '{0}'", ID);
            DataTable dr = DBHelperSQL.GetDataTable(sq);
            if (dr.Rows[0]["配送状态"].ToString()=="已配送")
            {
                return false;
            }
            else
            {
                sql = string.Format("update Orderss set [Status]='已配送'  where Orderid = '{0}'", ID);

            }


            bool sql1 = DBHelperSQL.ExecuteNonQuery(sql);
            return sql1;
        }

        //下单后添加到Order
        public static bool AddOrder(Orderss o)
        {
            string sql = string.Format("insert into Orderss Values ({0},'{1}',1,{2},'{3}',getdate(),'未配送',{4})",o.Uid,o.Phone,o.Allprice,o.Adds,o.Foodid);

            return DBHelperSQL.ExecuteNonQuery(sql);
        }
    }
}
